//
//  FMDBTools.m
//  Products_B
//
//  Created by lanou on 16/6/2.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "FMDBTools.h"
#import <FMDB.h>
static FMDatabase *db = nil;
@implementation FMDBTools

+ (FMDBTools *)shareInstance {
    static FMDBTools *tool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tool = [[FMDBTools alloc] init];
    });
    return tool;
}

// 打开数据库
- (void)openDB {
    if (db != nil) {
        return;
    }
    NSString *filePath = [self creatSqliteWithSqiliteName:@"address.sqilte"];
    db = [FMDatabase databaseWithPath:filePath];
    NSLog(@"filePath ===== %@",filePath);
    if ([db open]) {
        NSLog(@"打开数据库成功");
    } else {
        NSLog(@"打开数据库失败");
    }
}

#pragma mark 在数据库中创建表格
- (void)creatTableWithTableName:(NSString *)tableName {
    // 1、打开数据库
    [db open];
    // 2、创建sql语句
    NSString *string = [NSString stringWithFormat:@"create table if not exists %@ (ID integer primary key autoincrement, name text, phone text , address text ,ismoren integer)",tableName];
    BOOL flag = [db executeUpdate:string];
    NSLog(@"创建表flag:%d",flag);
    // 关闭数据库
    [db close];
}

#pragma mark - 插入(添加)
- (void)insertDataWithTableName:(NSString *)tableName name:(NSString *)name phone:(NSString *)phone address:(NSString *)address isMoren:(BOOL)isMoren {
    [db open];
    NSString *string = [NSString stringWithFormat:@"insert into %@ (name, phone, address, ismoren) values (?, ?, ?, ?)",tableName];
    NSString *boolSrting = [NSString stringWithFormat:@"%d",isMoren];
    BOOL flag = [db executeUpdate:string,name,phone,address,boolSrting];
    NSLog(@"插入%d",flag);
    [db close];
}

#pragma  - 删除 
- (void)deleteDataWithTableName:(NSString *)tableName phone:(NSString *)phone {
    [db open];
    NSString *string = [NSString stringWithFormat:@"delete from %@ where phone = ?",tableName];
    BOOL flag = [db executeUpdate:string,phone];
    NSLog(@"%d",flag);
    [db close];
}

#pragma mark - 更新
- (void)updateDataWithTbleName:(NSString *)tableName name:(NSString *)name phone:(NSString *)phone address:(NSString *)address isMoren:(BOOL)isMoren {
    [db open];
    NSString *string = [NSString stringWithFormat:@"update %@ set name = ?, phone = ?, address = ?, ismoren = ?",tableName];
    NSString *boolSrting = [NSString stringWithFormat:@"%d",isMoren];
    BOOL flag = [db executeUpdate:string,name,phone,address,boolSrting];
    NSLog(@"修改数据%d",flag);
    [db close];
}

#pragma mark - 查询一个
- (ChuanzhiModel *)selectOneDataWithTableName:(NSString *)tableName phone:(NSString *)phone {
    [db open];
    NSString *string = [NSString stringWithFormat:@"select * from %@ where phone = ?",tableName];
    FMResultSet *result = [db executeQuery:string,phone];
    ChuanzhiModel *model = [[ChuanzhiModel alloc] init];
    while ([result next]) {
        model.nameString = [result stringForColumn:@"name"];
        model.phoneString = [result stringForColumn:@"phone"];
        model.addressString = [result stringForColumn:@"address"];
        model.isMoren = [[result stringForColumn:@"ismoren"] integerValue];
        return model;
    }
    [db close];
    return model;
}

#pragma mark - 查询所有数组
- (NSMutableArray *)selectAllDataWthTableName:(NSString *)tableName {
    [db open];
    NSString *string = [NSString stringWithFormat:@"select * from %@",tableName];
    FMResultSet *result = [db executeQuery:string];
//    NSLog(@"%@",result);
    NSMutableArray *modelArray = [NSMutableArray array];
    while ([result next]) {
        ChuanzhiModel *model = [[ChuanzhiModel alloc] init];
        model.nameString = [result stringForColumn:@"name"];
        model.phoneString = [result stringForColumn:@"phone"];
        model.addressString = [result stringForColumn:@"address"];
        model.isMoren = [[result stringForColumn:@"ismoren"] integerValue];
        [modelArray addObject:model];
    }
    [db close];
    NSLog(@"%@",modelArray);
    return modelArray;
}

#pragma mark 设置数据库路径
- (NSString *)creatSqliteWithSqiliteName:(NSString *)sqliteName {
    return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:sqliteName];
}

@end








